using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.SLXNK
{
    /// <summary>
    /// Loại hình mậu dịch.
    /// </summary>
    public class LoaiHinhMauDich
    {
        public static DataTable SelectAll()
        {
            string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
            string query;
            query = "SELECT Ma_LH AS Ma, Ten_LH AS Ten, Ten_VT AS TenVT FROM SLHINHMD";
            DataTable dt = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query).Tables[0];
            foreach (DataRow r in dt.Rows)
            {
                r["Ten"] = FontConverter.TCVN2Unicode(r["Ten"].ToString());
            }
            DataRow row = dt.NewRow();
            row["Ma"] = "   ";
            row["Ten"] = "[Tất cả]";
            row["TenVT"] = "";
            dt.Rows.Add(row);
            return dt;
        }

        public static string GetTenVietTatAndTenLoaiHinh(string maLoaiHinh)
        {
            string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
            string query;
            query = "SELECT Ma_LH AS Ma, Ten_LH AS Ten, Ten_VT AS TenVT FROM SLHINHMD WHERE MA_LH = @MaLoaiHinh";
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@MaLoaiHinh", SqlDbType.Char, 5);
            parameters[0].Value = maLoaiHinh;
            SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, query, parameters);
            if (reader.Read())
            {
                return reader["TenVT"] + " - " + FontConverter.TCVN2Unicode(reader["Ten"].ToString());
            }
            return maLoaiHinh;
        }

        public static string GetTenLoaiHinh(string maLoaiHinh)
        {
            string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
            string query;
            query = "SELECT Ma_LH AS Ma, Ten_LH AS Ten, Ten_VT AS TenVT FROM SLHINHMD WHERE MA_LH = @MaLoaiHinh";
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@MaLoaiHinh", SqlDbType.Char, 5);
            parameters[0].Value = maLoaiHinh;
            SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, query, parameters);
            if (reader.Read())
            {
                return FontConverter.TCVN2Unicode(reader["Ten"].ToString());
            }
            return maLoaiHinh;
        }

        public static string GetTenVietTat(string maLoaiHinh)
        {
            string connectionString = ConfigurationManager.AppSettings["SLXNKConnectionString"];
            string query;
            query = "SELECT Ma_LH AS Ma, Ten_LH AS Ten, Ten_VT AS TenVT FROM SLHINHMD WHERE MA_LH = @MaLoaiHinh";
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@MaLoaiHinh", SqlDbType.Char, 5);
            parameters[0].Value = maLoaiHinh;

            SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, CommandType.Text, query, parameters);
            if (reader.Read())
            {
                return FontConverter.TCVN2Unicode(reader["TenVT"].ToString());
            }
            return maLoaiHinh;
        }
    }
}
